package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.module.system.domain.SysDictType;
import com.cheetah.start.module.system.service.ISysDictDataService;
import com.cheetah.start.module.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotBlank;

/**
 * 字典类型控制器
 *
 * @author Feel
 * @date 2021/07/25
 */
@Api(tags = "字典类型")
@Slf4j
@Validated
@RestController
@RequestMapping("/system/dictType")
public class SysDictTypeController extends ResultController {
    @Autowired
    private ISysDictTypeService dicTypeService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 字典
     *
     * @return {@link String}
     */
    @GetMapping()
    public ModelAndView listPage() {
        return jumpPage("/system/dict/list");
    }

    /**
     * 字典类型
     */
    @GetMapping("/add")
    public ModelAndView addPage() {
        return jumpPage("/system/dict/add");
    }


    /**
     * 编辑
     *
     * @param dictTypeId dict类型id
     * @return {@link ModelAndView}
     */
    @ApiOperation(value = "编辑字典类型页面")
    @GetMapping("/edit")
    public ModelAndView edit(String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        ModelAndView model = new ModelAndView();
        model.addObject("sysDictType", dictType);
        model.setViewName("/system/dict/edit");
        return model;
    }

    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @SaCheckPermission("sys:dictType:data")
    @ApiOperation(value = "字典类型列表")
    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery) {
        Page<SysDictType> page = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        dicTypeService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }

    /**
     * 添加
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:add")
    @ApiOperation(value = "添加字典类型")
    @PostMapping("/add")
    @Log(title = "字典类型", describe = "添加", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    public Result add(@Validated @RequestBody SysDictType sysDictType) {
        SysDictType one = dicTypeService.getByTypeCode(sysDictType.getTypeCode());
        if (one != null) {
            return failure("字典类型不能重复");
        }
        boolean save = dicTypeService.save(sysDictType);
        return decide(save);
    }


    /**
     * 更新
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:edit")
    @ApiOperation(value = "编辑字典类型")
    @PostMapping("/update")
    @Log(title = "字典类型", describe = "编辑", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result update(@Validated @RequestBody SysDictType sysDictType) {
        if (sysDictType.getDictTypeId() != null) {
            boolean b = dicTypeService.updateById(sysDictType);
            return decide(b, BusinessTypeEnum.EDIT);
        }
        return failure("请先添加");
    }


    /**
     * 删除
     *
     * @param dictTypeId dict类型id
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:delete")
    @ApiOperation(value = "删除字典类型")
    @PostMapping("/remove/{dictTypeId}")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "字典数据", describe = "删除", type = BusinessTypeEnum.REMOVE, logType = LogTypeEnum.OPERATE)
    public Result remove(@PathVariable @NotBlank(message = "不能为空") String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        String typeCode = dictType.getTypeCode();
        boolean b = dicTypeService.removeById(dictTypeId);
        dictDataService.removeByTypeCode(typeCode);
        return decide(b, BusinessTypeEnum.REMOVE);
    }

    /**
     * 启用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:switch")
    @ApiOperation(value = "启用字典类型")
    @PostMapping("/enable")
    @Log(title = "字典类型", describe = "启用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result enable(@RequestBody SysDictType sysDictType) {
        SysDictType byId = dicTypeService.getById(sysDictType.getDictTypeId());
        byId.setEnable("0");
        return decide(dicTypeService.updateById(byId), BusinessTypeEnum.EDIT);
    }


    /**
     * 禁用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:switch")
    @ApiOperation(value = "禁用字典类型")
    @PostMapping("/disable")
    @Log(title = "字典类型", describe = "禁用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result disable(@RequestBody SysDictType sysDictType) {
        SysDictType dictType = dicTypeService.getById(sysDictType.getDictTypeId());
        dictType.setEnable("1");
        return decide(dicTypeService.updateById(dictType), BusinessTypeEnum.EDIT);
    }

}
